<style lang="less">
@import "./main.less";
</style>
<template>
    <div class="main" :class="{'main-hide-text': shrink}">
        <div class="sidebar-menu-con" :style="{width: shrink?'60px':'200px', overflow: shrink ? 'visible' : 'auto'}">
            <shrinkable-menu 
                :shrink="shrink"
                @on-change="handleSubmenuChange"
                :theme="menuTheme" 
                :before-push="beforePush"
                :open-names="openedSubmenuArr"
                :menu-list="menuList">
                <div slot="top" class="logo-con">
                    <div class="logo-title" >采购管理系统</div>
                    <div class="login-info">
                        <img src="../images/userinfo.png" class="avtor">
                        <div class="login-name">{{userName.userName}}</div>
                        <div class="login-role">{{userName.userTypeDesc}}</div>
                    </div>
                </div>
            </shrinkable-menu>
        </div>
        <div class="main-header-con" :style="{paddingLeft: shrink?'60px':'200px'}">
            <div class="main-header">
                <div class="header-middle-con">
                    <div class="main-breadcrumb">
                        <breadcrumb-nav :currentPath="currentPath"></breadcrumb-nav>
                    </div>
                </div>
                <div class="header-avator-con">
                    <!-- <Button type="primary"  size="large" style="right: 120px;display: inline-block;margin-left: -40px;" @click="goWeixiu">我要报修</Button> -->
                    <full-screen v-model="isFullScreen" @on-change="fullscreenChange"></full-screen>
                   
                    <router-link :to="{name:'message'}" style="right: -40px;
                        cursor: pointer;
                        display: inline-block;
                        margin-left: 40px;
                        width: 32px;
                        height: 32px;">
                        <Badge :count="messageNum" type="error" >
                        <Icon size="24" type="android-notifications-none"/>
                      </Badge>
                    </router-link>
                    <!-- <lock-screen></lock-screen> -->
                    <div class="user-dropdown-menu-con">
                        <Row type="flex" justify="end" align="middle" class="user-dropdown-innercon">
                            <Dropdown transfer trigger="click" @on-click="handleClickUserDropdown">
                                <a href="javascript:void(0)">
                                    <span class="main-user-name">{{ userName.userName }}</span>
                                    <Icon type="arrow-down-b"></Icon>
                                </a>
                                <DropdownMenu slot="list">
                                    <DropdownItem name="ownSpace">修改密码</DropdownItem>
                                    <DropdownItem name="loginout" divided>退出登录</DropdownItem>
                                </DropdownMenu>
                            </Dropdown>
                        </Row>
                    </div>
                </div>
            </div>
            <div class="tags-con">
                <tags-page-opened :pageTagsList="pageTagsList"></tags-page-opened>
            </div>
        </div>
        <div class="single-page-con" :style="{left: shrink?'60px':'200px'}">
            <div class="single-page">
                <keep-alive :include="cachePage">   
                    <router-view></router-view>
                </keep-alive>
            </div>
        </div>
    </div>
</template>
<script>
import shrinkableMenu from "./main-components/shrinkable-menu/shrinkable-menu.vue";
import tagsPageOpened from "./main-components/tags-page-opened.vue";
import breadcrumbNav from "./main-components/breadcrumb-nav.vue";
import fullScreen from "./main-components/fullscreen.vue";
import lockScreen from "./main-components/lockscreen/lockscreen.vue";
import messageTip from "./main-components/message-tip.vue";
import themeSwitch from "./main-components/theme-switch/theme-switch.vue";
import util from "@/libs/util.js";
import { getMsgMy } from "@/actions/sys";
export default {
  components: {
    shrinkableMenu,
    tagsPageOpened,
    breadcrumbNav,
    fullScreen,
    lockScreen,
    messageTip,
    themeSwitch
  },
  data() {
    return {
      shrink: false,
      isFullScreen: false,
      openedSubmenuArr: this.$store.state.app.openedSubmenuArr,
      messageNum: 0,
      timer: null
    };
  },
  computed: {
    menuList() {
      return this.$store.state.app.menuList;
    },
    pageTagsList() {
      return this.$store.state.app.pageOpenedList; // 打开的页面的页面对象
    },
    currentPath() {
      return this.$store.state.app.currentPath; // 当前面包屑数组
    },
    avatorPath() {
      return localStorage.avatorImgPath;
    },
    cachePage() {
      return this.$store.state.app.cachePage;
    },
    lang() {
      return this.$store.state.app.lang;
    },
    menuTheme() {
      return this.$store.state.app.menuTheme;
    },
    mesCount() {
      return this.$store.state.app.messageCount;
    }
  },
  methods: {
    init() {
      let pathArr = util.setCurrentPath(this, this.$route.name);
      this.$store.commit("updateMenulist");
      if (pathArr.length >= 2) {
        this.$store.commit("addOpenSubmenu", pathArr[1].name);
      }
      this.checkTag(this.$route.name);
    },
    toggleClick() {
      this.shrink = !this.shrink;
    },
    handleClickUserDropdown(name) {
      if (name === "ownSpace") {
        util.openNewPage(this, "account-password");
        this.$router.push({
          name: "account-password"
        });
      } else if (name === "loginout") {
        // 退出登录
        this.$store.commit("logout", this);
        this.$store.commit("clearOpenedSubmenu");
        this.$router.push({
          name: "login"
        });
      }
    },
    checkTag(name) {
      let openpageHasTag = this.pageTagsList.some(item => {
        if (item.name === name) {
          return true;
        }
      });
      if (!openpageHasTag) {
        //  解决关闭当前标签后再点击回退按钮会退到当前页时没有标签的问题
        util.openNewPage(
          this,
          name,
          this.$route.params || {},
          this.$route.query || {}
        );
      }
    },
    handleSubmenuChange(val) {
      // console.log(val)
    },
    beforePush(name) {
      // if (name === 'accesstest_index') {
      //     return false;
      // } else {
      //     return true;
      // }
      return true;
    },
    fullscreenChange(isFullScreen) {
      // console.log(isFullScreen);
    },
    goWeixiu() {
      this.$router.push({
        name: "keep-apply-add",
        query: {
          name: this.$route.name
        }
      });
    },
    getMsgMy(flag) {
      getMsgMy().then(({ data }) => {
        if (!flag) {
          if (data.status) {
            this.messageNum = data.num;
            this.$Notice.config({
              top: 130,
              duration: 5
            });
            this.$Notice.open({
              title: "消息提醒",
              name: "my-message",
              desc: data.msg,
              render: h => {
                return h(
                  "div",
                  {
                    style: {
                      cursor: "pointer",
                      minHeight: "32px",
                      lineHeight: "32px"
                    },
                    on: {
                      click: () => {
                        this.$Notice.close("my-message");
                        this.$router.push({
                          name: "message"
                        });
                      }
                    }
                  },
                  data.msg
                );
              }
            });
          } else {
            this.messageNum = data.num || 0;
          }
        } else {
          this.messageNum = data.num || 0;
        }
      });
    },
    setTimer() {
      this.timer = setInterval(() => {
        this.getMsgMy();
      }, 60000);
    }
  },
  watch: {
    $route(to) {
      this.$store.commit("setCurrentPageName", to.name);
      let pathArr = util.setCurrentPath(this, to.name);
      if (pathArr.length > 2) {
        this.$store.commit("addOpenSubmenu", pathArr[1].name);
      }
      this.checkTag(to.name);
      localStorage.currentPageName = to.name;
    },
    lang() {
      util.setCurrentPath(this, this.$route.name); // 在切换语言时用于刷新面包屑
    }
  },

  mounted() {
    this.init();
  },
  distroyed() {
    clearInterval(this.timer);
  },
  created() {
    // 显示打开的页面的列表
    let name = this.$store.state.user.userInfo;
    this.userName = name;
    this.$store.commit("setOpenedList");
    this.getMsgMy();
    this.setTimer();
    this.bus.$on("toGetMsg", () => {
      this.getMsgMy("fromnews");
    });
  }
};
</script>
